﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Modbus;
using FtdAdapter;
using Modbus.Data;
using Modbus.Device;
using iCollect.Constants;
using System.Collections;
using iCollect.CustomComponent;
using iCollect.util;

namespace iCollect.CollectDetail
{
    public partial class QiyajiJK : BaseDeviceJK
    {

        protected Dictionary<String, TextBox> tbNI2Map = new Dictionary<String, TextBox>();
        private NIUtil niUtil1 = new NIUtil();
        private NIUtil niUtil2 = new NIUtil();

        public QiyajiJK()
        {
            InitializeComponent();

            setEvent();
        }

        protected override void createTextBoxMap()
        {
            tbModbusMap.Add("PIT3699", tbPIT3699);
            tbModbusMap.Add("PT3697", tbPT3697);
            tbModbusMap.Add("PIT3649", tbPIT3649);
            tbModbusMap.Add("PIT3652", tbPIT3652);
            tbModbusMap.Add("FIT3699", tbFIT3699);
            tbModbusMap.Add("FT3653", tbFT3653);
            tbModbusMap.Add("FT3649", tbFT3649);
            tbModbusMap.Add("TE3692", tbTE3692);
            tbModbusMap.Add("TE3629A", tbTE3629A);
            tbModbusMap.Add("TE3630A", tbTE3630A);
            tbModbusMap.Add("TE3694", tbTE3694);

            tbNIMap.Add("VT3696X", tbVT3696X);
            tbNIMap.Add("VT3697Y", tbVT3697Y);
            tbNIMap.Add("VT3698X", tbVT3698X);
            tbNIMap.Add("VT3699Y", tbVT3699Y);
            tbNIMap.Add("VT3629X", tbVT3629X);
            tbNIMap.Add("VT3629Y", tbVT3629Y);
            tbNIMap.Add("VT3630X", tbVT3630X);
            tbNIMap.Add("VT3630Y", tbVT3630Y);

            tbNI2Map.Add("ZE3696", tbZE3696);
            tbNI2Map.Add("ZE3697", tbZE3697);
            tbNI2Map.Add("ZE3629A", tbZE3629A);
            tbNI2Map.Add("ZE3629B", tbZE3629B);

            niUtil1.initTask(tbNIMap.Keys.ToArray<String>());
            niUtil2.initTask(tbNI2Map.Keys.ToArray<String>());
        }

        override public void loadDevice()
        {
            niUtil1.initTask(tbNIMap.Keys.ToArray<String>());
            niUtil2.initTask(tbNI2Map.Keys.ToArray<String>());
        }

        override protected void readFromDevice()
        {
            ModbusSerialRtuMasterReadRegisters();
            niUtil1.readData();
            niUtil2.readData();
        }

        override protected void displayValue()
        {
            base.displayValue();
            for (int i = 0; i < tbNI2Map.Keys.Count; i++)
            {
                string key = tbNI2Map.Keys.ElementAt(i);
                List<ArrayList> itemList = GlobalConstants.currentData[key];
                ArrayList timeValue = itemList[itemList.Count - 1];
                Double channelValue = Convert.ToDouble(timeValue[1]);
                setTextBoxValue(tbNI2Map, channelValue, key);
            }
        }

    }
}
